package object_test;


public class CloneableTest {
    public static void main(String[] args) {
        Animal animal1 = new Animal();
        // animal2是通过animal1克隆复制来的，确实是两个独立的对象，属性值完全相同
        Animal animal2 = animal1.clone();
        System.out.println(animal1 == animal2);
    }
}

class Animal implements Cloneable{
    public Animal() {
        System.out.println("Animal类的无参构造");
    }
    String name;

    public Animal(String name) {
        this.name = name;
    }

    public Animal clone() {
        Animal newAnimal = null;
        try {
            newAnimal = (Animal) super.clone();
        } catch (CloneNotSupportedException e) {
            throw new RuntimeException(e);
        }
        return newAnimal;
    }
}